package renshi.action;

import java.sql.Connection;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import renshi.base.BaseAction;
import renshi.dao.AdminDAO;
import renshi.dao.StudentDAO;
import renshi.dao.TeacherDAO;
import renshi.model.Student;
import renshi.model.Tclass;
import renshi.model.Teacher;
import renshi.utils.DBUtils;

public class LoginAction extends BaseAction{
	
	private static final long serialVersionUID = 1L;
	private String loginname; 
	private String pwd;
	private int role;	
	public String getLoginname() {
		return loginname;
	}
	public void setLoginname(String loginname) {
		this.loginname = loginname;
	}
	public String getPwd() {
		return pwd;
	}
	public void setPwd(String pwd) {
		this.pwd = pwd;
	}
	public int getRole() {
		return role;
	}
	public void setRole(int role) {
		this.role = role;
	}
	
	public String execute() throws Exception {
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpSession session = request.getSession();
		Connection con = DBUtils.getConnection();
        con.setAutoCommit(false);
		
		if(role==0)//ѧ���¼
			{
		        StudentDAO studentDAO = new StudentDAO(con);
		        
		        boolean flag = studentDAO.login(loginname, pwd);
		        con.commit();
		        if(flag==true){
		        	session.setAttribute("loginname",loginname);
		        	Student student = studentDAO.querystudent(loginname);
		        	request.setAttribute("student", student);
		        	System.out.println("loginAction��lllllllllllllllll");
		            if(student!=null){
		            	System.out.println("student==null��lllllllllllllllll");
		            	
		            	Tclass tclass=studentDAO.queryteacher(student.getCname());
		            	request.setAttribute("tclass", tclass);
		            	
		            	return "studentSuccess";     
		            	
		            }
		        	else
		            	return "queryfail";
		            	   
		           }
		        	else
		            	return "loginFail";
		        	
		        }
		else if(role==1){//��ʦ��¼
			openCon();
			con.setAutoCommit(false);
			TeacherDAO teacherDAO = new TeacherDAO(con);
			boolean flag = teacherDAO.query(loginname, pwd);
			con.commit();
			if(flag==true){
				session.setAttribute("loginname",loginname);
				Teacher teacher=teacherDAO.queryturename(loginname);
				request.setAttribute("teacher",teacher);
				if(teacher!=null){
					String cname=teacherDAO.querycname(teacher.getTurename());
					session.setAttribute("cname", cname);
					return "teacherSuccess";
				}
				else
					return "queryFail";
			}
			else
				return "loginFail";
		}else{//����Ա��¼
			AdminDAO adminDAO = new AdminDAO(con);
			boolean flag = adminDAO.login(loginname, pwd);
			con.commit();
			if(flag==true){
				session.setAttribute("loginname",loginname);
				return "adminSuccess";
			}
			else
				return "loginFail";
		}		
	}	
	
	public String querystudent()throws Exception{
		Connection con = DBUtils.getConnection();
        con.setAutoCommit(false);
		
		StudentDAO studentDAO = new StudentDAO(con);
		Student student = studentDAO.querystudent(loginname);
        con.commit();
        if(student!=null){
        	return "querystudentSuccess";
        }
    	else
        	return "queryfail";
		
	}
	
	public String update(){
		return "updateSuccess";
	}
}
